<template>
  <div class="app-container">
    <div v-if="!detailInfo.visible">
      <page-filter
        v-model:query="filterInfo.query"
        :filter-list="filterInfo.list"
      >
        <template v-slot:filter-button>
          <el-button
            v-if="$permission('saleReturnOrder:update')"
            :disabled="
              !tableInfo.selections.length ||
              !!tableInfo.selections.filter((item) => item.status !== 3).length
            "
            @click="makeSure()"
            style="margin-right: 10px"
            >确认</el-button
          >
          <el-button
            v-if="$permission('saleReturnOrder:update')"
            :disabled="
              !tableInfo.selections.length ||
              !!tableInfo.selections.filter((item) => item.status !== 4).length
            "
            @click="receive()"
            style="margin-right: 10px"
            >收货</el-button
          >
        </template>
      </page-filter>
      <table-container
        :refresh="tableInfo.refresh"
        :init-curpage="tableInfo.initCurpage"
        v-model:data="tableInfo.data"
        :query="filterInfo.query"
        :api="tableInfo.api"
        :field-list="tableInfo.fieldList"
        :handle="tableInfo.handle"
        @handleEvent="handleEvent"
      >
        <template v-slot:col-orderNo="scope">
          <span class="slip-edit" @click="showDetail(scope.row)">{{
            scope.row.orderNo
          }}</span>
        </template>
      </table-container>
    </div>
    <order-details
      v-if="detailInfo.visible"
      v-model:visible="detailInfo.visible"
      :orderId="detailInfo.id"
      @callback="modifyCallBack"
    ></order-details>
  </div>
</template>

<script setup name="saleReturnOrder">
import PageFilter from "@/components/PageFilter/index";
import TableContainer from "@/components/TableContainer/index";
import { getOrgAll } from "@/api/base";
import {
  getSaleReturnPage,
  saleReturnConfirm,
  saleReturnReject,
  saleReturnRefuse,
  saleReturnReceive,
} from "@/api/supplier";
import {
  rejectSaleReturnOrder,
  getSaleReturnOrder,
  checkSaleReturnOrder,
  receiveSaleReturnOrder,
  refuseSaleReturnOrder,
} from "@/api/purch";
import OrderDetails from "./detail";

const { proxy } = getCurrentInstance();

const filterInfo = reactive({
  query: {
    sort: "create_time desc",
    statusNotIn: 1,
    supplierAuth: "supplierId",
    isSupplier: true,
  },
  list: [
    { value: "button", type: "slot" },
    {
      type: "input",
      label: "退货单号",
      value: "orderNoLike",
      event: search,
    },
    { type: "button", label: "搜索", event: search },
  ],
});
const listTypeInfo = reactive({
  orgList: [],
});
const tableInfo = reactive({
  refresh: 1,
  initCurpage: 1,
  api: getSaleReturnOrder,
  data: [],
  selections: [],
  fieldList: [
    { label: "退货单号", value: "orderNo", type: "slot" },
    { label: "客户", value: "orgName" },
    { label: "经手人", value: "updateUserName" },
    { label: "申请时间", value: "createTime" },
    { label: "状态", value: "statusValue" },
    { label: "备注", value: "remark" },
  ],
  handle: {
    fixed: "right",
    label: "操作",
    width: '100',
    btList: [
      {
        label: "确认",
        event: makeSure,
        show: proxy.$permission("saleReturnOrder:update"),
        ifRender(row) {
          return row.status === 3;
        },
      },
      {
        label: "拒绝",
        event: reject,
        show: proxy.$permission("saleReturnOrder:update"),
        ifRender(row) {
          return row.status === 3;
        },
      },
      {
        label: "收货",
        event: receive,
        show: proxy.$permission("saleReturnOrder:update"),
        ifRender(row) {
          return row.status === 4;
        },
      },
      {
        label: "拒收",
        event: refuse,
        show: proxy.$permission("saleReturnOrder:update"),
        ifRender(row) {
          return row.status === 4;
        },
      },
    ],
  },
});
const detailInfo = reactive({
  visible: false,
  id: "",
});

onMounted(() => {
  init();
});

function init() {
  let params = {
    sort: "create_time desc",
    searchType: "org",
    hospital: true,
  };
  getOrgAll(params).then((resp) => {
    if (resp && resp.success) {
      listTypeInfo.orgList = resp.data.map((item) => {
        return {
          key: item.name,
          value: item.id,
        };
      });
    }
  });
}
function getList() {
  tableInfo.refresh = Math.random();
}
function search() {
  tableInfo.initCurpage = Math.random();
  tableInfo.refresh = Math.random();
}
function getIds(row) {
  if (row) {
    return [row.id];
  } else {
    return tableInfo.selections.map((value) => value.id);
  }
}
function makeSure(row) {
  const ids = getIds(row);
  if (ids.length) {
    checkSaleReturnOrder(ids).then((res) => {
      if (res) {
        getList();
      }
    });
  }
}
//拒绝
function reject(row) {
  proxy.$prompt("拒绝理由", "销售退货单", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    inputPattern: /^[\S]+/,
    inputErrorMessage: "请输入拒绝理由",
  })
    .then(({ value }) => {
      let params = {};
      params.id = row.id;
      params.rejectRemark = value;

      rejectSaleReturnOrder(params).then((res) => {
        if (res) {
          getList();
        }
      });
    })
    .catch(() => {});
}
function receive(row) {
  const ids = getIds(row);
  if (ids.length) {
    receiveSaleReturnOrder(ids).then((res) => {
      if (res) {
        getList();
      }
    });
  }
}
//拒收
function refuse(row) {
  proxy.$prompt("拒收理由", "销售退货单", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    inputPattern: /^[\S]+/,
    inputErrorMessage: "请输入拒收理由",
  })
    .then(({ value }) => {
      let params = {};
      params.id = row.id;
      params.rejectRemark = value;

      refuseSaleReturnOrder(params).then((res) => {
        if (res) {
          getList();
        }
      });
    })
    .catch(() => {});
}
function handleEvent(event, data) {
  switch (event) {
    case "tableCheck":
      tableInfo.selections = data;
      break;
  }
}
function showDetail(row) {
  detailInfo.visible = true;
  detailInfo.id = row.id;
}
function modifyCallBack(data) {
  if (data) {
    getList();
  }
  detailInfo.visible = false;
}
</script>

<style scoped></style>
